//
//  QQSevrivceViewController.m
//  51易借
//
//  Created by 张睿 on 2017/9/25.
//  Copyright © 2017年 张睿. All rights reserved.
//

#import "QQSevrivceViewController.h"

#define SYS_DEVICE_WIDTH    ([[UIScreen mainScreen] bounds].size.width)                  // 屏幕宽度

#define SYS_DEVICE_HEIGHT   ([[UIScreen mainScreen] bounds].size.height)                 // 屏幕长度

#define MIN_HEIGHT          50                                                          // 图形最小高度

@interface QQSevrivceViewController ()

@property (nonatomic, assign) CGFloat mHeight;

@property (nonatomic, assign) CGFloat curveX;               // r5点x坐标

@property (nonatomic, assign) CGFloat curveY;               // r5点y坐标

@property (nonatomic, strong) UIView *curveView;            // r5红点

@property (nonatomic, strong) CAShapeLayer *shapeLayer;

@property (nonatomic, strong) CADisplayLink *displayLink;

@property (nonatomic, assign) BOOL isAnimating;


@property (nonatomic, weak)UIScrollView * scrollView;



@end

@implementation QQSevrivceViewController

static NSString *kX = @"curveX";

static NSString *kY = @"curveY";

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"aaaa";
    self.view.backgroundColor = [UIColor grayColor];
    
//    [self addObserver:self forKeyPath:kX options:NSKeyValueObservingOptionNew context:nil];
//    
//    [self addObserver:self forKeyPath:kY options:NSKeyValueObservingOptionNew context:nil];
//    
//    [self configShapeLayer];
//    
//    [self configCurveView];
//    
//    [self configAction];
    
    [self setupUI];
}


- (void)setupUI{
    
//    UIView * view = [[UIView alloc] initWithFrame:self.view.bounds];
//    
//    view.backgroundColor = [UIColor grayColor];
//    
//    [self.view addSubview:view];
    
    UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    
    scrollView.backgroundColor = [UIColor redColor];
    //    水平方向
    scrollView.showsHorizontalScrollIndicator = NO;
    //    垂直方向
    scrollView.showsVerticalScrollIndicator = NO;
    //    不管有没有设置contentSize,总是有弹簧效果(下拉刷新)
    scrollView.alwaysBounceHorizontal = NO;
    scrollView.alwaysBounceVertical = YES;
    //    是否有弹簧效果
    scrollView.bounces = YES;
    //    scrollView.contentSize = self.view.bounds.size;
    
    self.scrollView = scrollView;
    
    [self.view addSubview:self.scrollView];

    
    UIView * view = [[UIView alloc] initWithFrame:self.view.bounds];
    
    view.backgroundColor = [UIColor blueColor];
    
    [self.scrollView addSubview:view];
    
}

//- (void)dealloc {
//    
//    [self removeObserver:self forKeyPath:kX];
//    
//    [self removeObserver:self forKeyPath:kY];
//    
//}



- (void)drawRect:(CGRect)rect

{
    
    
    
}



-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    
    if ([keyPath isEqualToString:kX] || [keyPath isEqualToString:kY]) {
        
        [self updateShapeLayerPath];
        
    }
    
}



#pragma mark -

#pragma mark - Configuration



- (void)configAction

{
    
    _mHeight = 100;                       // 手势移动时相对高度
    
    _isAnimating = NO;                    // 是否处于动效状态
    
    
    
    // 手势
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanAction:)];
    
    self.view.userInteractionEnabled = YES;
    
    [self.view addGestureRecognizer:pan];
    
    
    
    // CADisplayLink默认每秒运行60次calculatePath是算出在运行期间_curveView的坐标，从而确定_shapeLayer的形状
    
    _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(calculatePath)];
    
    [_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    
    _displayLink.paused = YES;
    
}



- (void)configShapeLayer

{
    
    _shapeLayer = [CAShapeLayer layer];
    
    //    _shapeLayer.fillColor = [UIColor colorWithRed:57/255.0 green:67/255.0 blue:89/255.0 alpha:1.0].CGColor;
    
    _shapeLayer.fillColor = [UIColor redColor].CGColor;
    
    [self.view.layer addSublayer:_shapeLayer];
    
}



- (void)configCurveView

{
    
    // _curveView就是r5点
    
    self.curveX = SYS_DEVICE_WIDTH/2.0;       // r5点x坐标
    
    self.curveY = MIN_HEIGHT;                 // r5点y坐标
    
    _curveView = [[UIView alloc] initWithFrame:CGRectMake(_curveX, _curveY, 3, 3)];
    
    _curveView.backgroundColor = [UIColor redColor];
    
    [self.view addSubview:_curveView];
    
}



#pragma mark -

#pragma mark - Action



- (void)handlePanAction:(UIPanGestureRecognizer *)pan

{
    
    if(!_isAnimating)
        
    {
        
        if(pan.state == UIGestureRecognizerStateChanged)
            
        {
            
            // 手势移动时，_shapeLayer跟着手势向下扩大区域
            
            CGPoint point = [pan translationInView:self.view];
            
            
            
            // 这部分代码使r5红点跟着手势走
            
            _mHeight = point.y*0.7 + MIN_HEIGHT;
            
            self.curveX = SYS_DEVICE_WIDTH/2.0 + point.x;
            
            self.curveY = _mHeight > MIN_HEIGHT ? _mHeight : MIN_HEIGHT;
            
            _curveView.frame = CGRectMake(_curveX,
                                          
                                          _curveY,
                                          
                                          _curveView.frame.size.width,
                                          
                                          _curveView.frame.size.height);
            
        }
        
        else if (pan.state == UIGestureRecognizerStateCancelled ||
                 
                 pan.state == UIGestureRecognizerStateEnded ||
                 
                 pan.state == UIGestureRecognizerStateFailed)
            
        {
            
            // 手势结束时,_shapeLayer返回原状并产生弹簧动效
            
            _isAnimating = YES;
            
            _displayLink.paused = NO;           //开启displaylink,会执行方法calculatePath.
            
            
            
            // 弹簧动效
            
            [UIView animateWithDuration:1.0
             
                                  delay:0.0
             
                 usingSpringWithDamping:0.5
             
                  initialSpringVelocity:0
             
                                options:UIViewAnimationOptionCurveEaseInOut
             
                             animations:^{
                                 
                                 
                                 
                                 // 曲线点(r5点)是一个view.所以在block中有弹簧效果.然后根据他的动效路径,在calculatePath中计算弹性图形的形状
                                 
                                 _curveView.frame = CGRectMake(SYS_DEVICE_WIDTH/2.0, MIN_HEIGHT, 3, 3);
                                 
                                 
                                 
                             } completion:^(BOOL finished) {
                                 
                                 
                                 
                                 if(finished)
                                     
                                 {
                                     
                                     _displayLink.paused = YES;
                                     
                                     _isAnimating = NO;
                                     
                                 }
                                 
                                 
                                 
                             }];
            
        }
        
    }
    
}



- (void)updateShapeLayerPath

{
    
    // 更新_shapeLayer形状
    
    UIBezierPath *tPath = [UIBezierPath bezierPath];
    
    [tPath moveToPoint:CGPointMake(0, 0)];                              // r1点
    
    [tPath addLineToPoint:CGPointMake(SYS_DEVICE_WIDTH, 0)];            // r2点
    
    [tPath addLineToPoint:CGPointMake(SYS_DEVICE_WIDTH,  MIN_HEIGHT)];  // r4点
    
    [tPath addQuadCurveToPoint:CGPointMake(0, MIN_HEIGHT)
     
                  controlPoint:CGPointMake(_curveX, _curveY)]; // r3,r4,r5确定的一个弧线
    
    [tPath closePath];
    
    _shapeLayer.path = tPath.CGPath;
    
}





- (void)calculatePath

{
    
    // 由于手势结束时,r5执行了一个UIView的弹簧动画,把这个过程的坐标记录下来,并相应的画出_shapeLayer形状
    
    CALayer *layer = _curveView.layer.presentationLayer;
    
    self.curveX = layer.position.x;
    
    self.curveY = layer.position.y;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
